package com.health.mapp.impl;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.health.mapp.bean.Header;
import com.health.mapp.bean.VerifyCodeReq;
import com.health.mapp.bean.VerifyCodeResp;
import com.health.mapp.utils.Response;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

public class VerifyCodeImpl implements ActionInterface {

	private static Logger logger = Logger.getLogger(VerifyCodeImpl.class);

	
	
	@Override
	public Response execute(Header header, Object reqBean) throws SQLException, Exception {
		VerifyCodeReq req = (VerifyCodeReq) reqBean;
		VerifyCodeResp resp = new VerifyCodeResp();
		 
		
	 
		
		String mobileNum = req.getMobileNum();	
		
		Date dt=new Date();
	     SimpleDateFormat matter1=new SimpleDateFormat("yyyy-MM-dd");
	    
		  //key  mobileNum+matter1.format(dt)
	     int count= 0;
	    Object redisCount = 0;
	    
	       
	       if(count>5)
	       {
	    	   resp.setErrorCode(1);
	    	   resp.setErrorMsg("发送短信次数超过限制请24小时后重试");
	    	   return resp;
	       }
	       else
	       {
	    	  
	       }
	     
	     
		
        String radom = String.valueOf(Math.round(Math.random()*9000+1000));

		TaobaoClient client = new DefaultTaobaoClient("http://gw.api.taobao.com/router/rest", "23305020", "c2fded7eadd12a6e8a7aa82253e4a23b");
		AlibabaAliqinFcSmsNumSendRequest alireq = new AlibabaAliqinFcSmsNumSendRequest();
		alireq.setExtend(radom);
		alireq.setSmsType("normal");
		alireq.setSmsFreeSignName("车游天下");
		alireq.setSmsParamString("{\"code\":\""+radom+"\",\"product\":\"车游天下\"}");
		alireq.setRecNum(mobileNum);
		alireq.setSmsTemplateCode("SMS_5080145");
		
		try{
			AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(alireq);
			String response =rsp.getBody();
			JSONObject jsStr = JSONObject.fromObject(response);
			JSONObject resultStr =JSONObject.fromObject( jsStr.get("alibaba_aliqin_fc_sms_num_send_response"));
			JSONObject reStr  =JSONObject.fromObject( resultStr.get("result"));
		    if (String.valueOf("0").equals((String) reStr.get("err_code")))
		    {
			 resp.setVerifyCode(radom);
			 resp.setMobileNum(mobileNum);
			 
			 count++;
		 
		
			 
		    }
		    else
		    {
			 resp.setErrorCode(1);
			 resp.setErrorMsg("发送失败请稍后重试");
		    }
		}catch (Exception e) {
	    	 resp.setErrorCode(1);
			 resp.setErrorMsg("发送失败请稍后重试");	// TODO: handle exception
		}
		return resp;
	}

	public static void main(String[] args) throws ApiException {
		/*TaobaoClient client = new DefaultTaobaoClient("http://gw.api.taobao.com/router/rest", "23305020", "c2fded7eadd12a6e8a7aa82253e4a23b");
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		req.setExtend("1234");
		req.setSmsType("normal");
		req.setSmsFreeSignName("登录验证");
		req.setSmsParamString("{\"code\":\"1234\",\"product\":\"车游天下\"}");
		req.setRecNum("15312980126");
		req.setSmsTemplateCode("SMS_5080145");
		AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
		String response =rsp.getBody();
		  JSONObject jsStr = JSONObject.fromObject(response);
		  JSONObject resultStr =JSONObject.fromObject( jsStr.get("alibaba_aliqin_fc_sms_num_send_response"));
		  JSONObject reStr    =JSONObject.fromObject( resultStr.get("result"));
		  int errid = Integer.parseInt((String) reStr.get("err_code"));
		  System.out.println(errid);*/
		  
		  
		  String radom = String.valueOf(Math.round(Math.random()*9000+1000));
		  System.out.println(radom);
	}
}
